﻿using System.Globalization;

namespace System {
    public static partial class Extention {
        ///   <summary> 
        ///  获取某一日期是该年中的第几周
        ///   </summary> 
        ///   <param name="dateTime"> 日期 </param> 
        ///   <returns> 该日期在该年中的周数 </returns> 
        public static int GetWeekOfYear(this DateTime dateTime) {
            GregorianCalendar gc = new GregorianCalendar();
            return gc.GetWeekOfYear(dateTime,CalendarWeekRule.FirstDay,DayOfWeek.Monday);
        }

        /// <summary>
        /// 获取Js格式的timestamp
        /// </summary>
        /// <param name="dateTime">日期</param>
        /// <returns></returns>
        public static long ToJsTimestamp(this DateTime dateTime) {
            var startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970,1,1,0,0,0,0));
            long result = (dateTime.Ticks - startTime.Ticks) / 10000;   //除10000调整为13位
            return result;
        }

        /// <summary>
        /// 获取js中的getTime()
        /// </summary>
        /// <param name="dt">日期</param>
        /// <returns></returns>
        public static Int64 JsGetTime(this DateTime dt) {
            Int64 retval = 0;
            var st = new DateTime(1970,1,1);
            TimeSpan t = (dt.ToUniversalTime() - st);
            retval = (Int64)(t.TotalMilliseconds + 0.5);
            return retval;
        }

        /// <summary>
        /// 返回默认时间1970-01-01
        /// </summary>
        /// <param name="dt">时间日期</param>
        /// <returns></returns>
        public static DateTime Default(this DateTime dt) {
            return DateTime.Parse("1970-01-01");
        }
        /// <summary>
        /// 得到某年的第一天
        /// </summary>
        /// <param name="year">年</param>
        /// <returns>返回第一天的日期</returns>
        public static DateTime GetFirstDateOfYear(int year) {
            return new DateTime(year,1,1);
        }
        /// <summary>
        /// 当月第一天
        /// </summary>
        /// <param name="Year"></param>
        /// <param name="Month"></param>
        /// <returns></returns>
        public static DateTime GetFirstDayOfMonth(int Year,int Month) {
            if(Month < 1) Month = 1;
            if(Month > 12) Month = 12;
            return new DateTime(Year,Month,1);
        }
        /// <summary>
        /// 当月最后一天
        /// </summary>
        /// <param name="Year"></param>
        /// <param name="Month"></param>
        /// <returns></returns>
        public static DateTime GetLastDayOfMonth(int Year,int Month) {
            if(Month < 1) Month = 1;
            if(Month > 12) Month = 12;
            return (new DateTime(Year,Month,1)).AddMonths(1).AddDays(-1);
        }
        /// <summary>
        /// 得到某日所在周的第一天
        /// </summary>
        /// <param name="dt">日期</param>
        /// <returns>返回某日所在周的第一天</returns>
        public static DateTime GetFirstDateOfWeek(DateTime dt) {
            int days = (int)dt.DayOfWeek;
            TimeSpan ts = new TimeSpan(days,0,0,0);
            return dt.Date - ts;
        }
        /// <summary>
        /// 得到本年已经过去的天数
        /// </summary>
        /// <returns>返回本年已经过去的天数</returns>
        public static int GetThisYearPastDays() {
            int days;
            DateTime today = DateTime.Today;
            DateTime firstDayOfCurYear = GetFirstDateOfYear(today.Year);
            TimeSpan ts = today - firstDayOfCurYear;
            days = (int)ts.TotalDays;
            if(days < 1) days = 1;
            return days;
        }

        /// <summary>
        /// 得到本周已经过去的天数
        /// </summary>
        /// <returns>返回本周已经过去的天数</returns>
        public static int GetThisWeekPastDays() {
            int days;
            DateTime today = DateTime.Today;
            DateTime firstDayOfCurWeek = GetFirstDateOfWeek(today);
            TimeSpan ts = today - firstDayOfCurWeek;
            days = (int)ts.TotalDays;
            if(days < 1) days = 1;
            return days;
        }

        /// <summary>
        /// yyyy-MM-dd
        /// </summary>
        public static string ToString5(this DateTime dt) {
            return (dt == new DateTime() ? string.Empty : dt.ToString("yyyy-MM-dd"));
        }
        /// <summary>
        /// yyyy-MM-dd HH:mm:ss
        /// </summary>
        public static string ToString6(this DateTime dt) {
            return (dt == new DateTime() ? string.Empty : dt.ToString("yyyy-MM-dd HH:mm:ss"));
        }
        /// <summary>
        /// yyyyMMdd
        /// </summary>
        public static string ToString8(this DateTime dt) {
            return (dt == new DateTime() ? string.Empty : dt.ToString("yyyyMMdd"));
        }
        /// <summary>
        /// yyyyMMddHHmmssfff
        /// </summary>
        public static string ToString7(this DateTime dt) {
            return (dt == new DateTime() ? string.Empty : dt.ToString("yyyyMMddHHmmssfff"));
        }
        /// <summary>
        ///  HH:mm:ss
        /// </summary>
        public static string ToString9(this DateTime dt) {
            return (dt == new DateTime() ? string.Empty : dt.ToString("HH:mm:ss"));
        }
        /// <summary>
        ///  HH:mm
        /// </summary>
        public static string ToString10(this DateTime dt)
        {
            return (dt == new DateTime() ? string.Empty : dt.ToString("HH:mm"));
        }
    }
}
